import axios from 'axios';
import { message } from 'ant-design-vue'

const myAxios = axios.create({
  timeout: 1000 * 6,
  withCredentials: true,
});

myAxios.interceptors.request.use(
  function (config) {
    return config;
  },
  function (error) {
    return Promise.reject(error);
  },
);

myAxios.interceptors.response.use(
  function (response) {
    const { data } = response;
    // 未登录
    if (data.code === 40100) {
      // 不是获取用户信息的请求，并且用户目前不是已经在用户登录页面，则跳转到登录页面
      if (
        !response.request.responseURL.includes('/user/get/login') &&
        !window.location.pathname.includes('/user/login')
      ) {
        message.warning('请登录');
        window.location.href = `/user/login?redirect=${window.location.href}`;
      }
    }
    return response;
  },
  function(error) {
    return Promise.reject(error);
  },
);

export default myAxios;
